<template>
  <el-form ref="form" :model="loginForm" size="large" class="formInput">
    <el-form-item prop="certId">
      <el-select v-model="form.certId" placeholder="请选择证书ID">
        <el-option v-for="(item, index) in cert" :key="index" :label="item" :value="item"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="pin">
      <el-input placeholder="请输入证书口令" type="passpassword" v-model="form.pin"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="getcert">获取证书</el-button>
      <el-button @click="login" type="primary">登录</el-button>
    </el-form-item>
    <!-- <el-form-item label="加密原文：" prop="origion">
        <el-input placeholder="请输入原文" v-model="form.origion"></el-input>
      </el-form-item>
      <el-form-item label="签名证书：" prop="certstr">
        <el-input
          placeholder="请输入签名证书"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 6 }"
          resize="none"
          :disabled="true"
          v-model="form.certstr"
        ></el-input>
      </el-form-item>
      <el-form-item label="签名值：" prop="changedata">
        <el-input
          placeholder="请输入签名值"
          :disabled="true"
          type="textarea"
          resize="none"
          :autosize="{ minRows: 2, maxRows: 6 }"
          v-model="form.changedata"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="sign">P7签名</el-button>
        <el-button @click="checkFn" type="primary">P7验证</el-button>
      </el-form-item> -->
  </el-form>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
import security from "@/utils/securityPlugin.js";



export default {
  name: "App",
  data() {
    return {
      cert: [],
      pin: "",
      form: {
        pin: "",
        certId: "",
        origion: "This is a test",
        certstr: "",
        changedata: "",
      },
      loginFlag: false,
    };
  },
  mounted() {
    console.log(security);
    // getcert();
  },
  methods: {
    getcert() {
      security.SOF_InitialVar("CERTID", "CertSelectPolicy", "1");
      security.SOF_InitialVar("CERTID", "KeyLoginPolicy", "0");
      this.cert = [];
      let userList = security.SOF_GetUserList();
      console.log(userList);
      this.cert = this.distinct(this.cert, userList.split("&&&"));
      if (this.cert && this.cert.length > 0) {
        this.form.certId = this.cert[0];
      }
    },
    distinct(a, b) {
      let arr = a.concat(b);
      return arr.filter((item, index) => {
        return arr.indexOf(item) === index;
      });
    },
    login() {
      // console.log('登录的ID为' + this.form.certId);
      // this.$emit('SdCALoginCheck', this.form.certId);
      let res = security.SOF_Login(this.form.certId, this.form.pin);

      if (res == 0) {
        let res = security.SOF_ShowSealData(this.form.certId, 0, 2);
        let signCert = security.SOF_ExportUserCert(this.form.certId);
        //获取证书序列号
        let zsxlh = security.SOF_GetCertInfo(signCert, 2);
        //加密数据
        const data = {
          "certId": this.form.certId,
          "username": zsxlh,
          "base64Image": res
        }
        this.$emit('SdCALoginCheck', data);
      } else {
        this.loginFlag = false;
        this.$message.error("证书登录失败！");
      }
      // SOF_Login;
    },
    getError() {
      return security.SOF_GetLastError();
    },
    sign() {
      if (this.loginFlag) {
        const certStr = security.SOF_ExportUserCert(this.form.certId);
        if (this.getError() == 0) {
          this.form.certstr = certStr;
          const signValue = security.SOF_SignDataByP7(
            this.form.certId,
            this.form.origion
          );
          if (this.getError() == 0) {
            this.form.changedata = signValue;
          } else {
            this.$message.error("证书签名失败！");
          }
        } else {
          this.$message.error("证书导出失败！");
        }
      } else {
        this.$message.error("请先进行登录！");
      }
    },
    checkFn() {
      if (this.form.certstr) {
        const flag = security.SOF_VerifySignedDataByP7(this.form.changedata); //证书、原文、签名值
        if (flag) {
          this.$message({
            message: "验证签名成功！",
            type: "success",
          });
        } else {
          this.$message({
            message: "验证签名失败！",
            type: "success",
          });
        }
      } else {
        this.$message.error("请先进行签名！");
      }
    },
  },
};
</script>

<style>
</style>
